package com.qdaily.ui.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.qdaily.supportlibrary.util.ImageUtil;
import com.qdaily.supportlibrary.util.LocalDisplay;
import com.qdaily.ui.R;
import com.qdaily.ui.data.DataBase.PraiseDAO;
import com.qdaily.ui.net.QDEnum;
import com.qdaily.ui.net.model.QuestionDetailOptions;

import java.util.*;

/**
 * Created by song on 15/5/19.
 */
public class MobListAdapter extends BaseAdapter implements View.OnClickListener {
    private Context mContext;
    private int[] mobOptionsBgColor = {};
    private int currentOptionsBgIndex = 0;
    private ArrayList<QuestionDetailOptions> list;
    private Set<Integer> optionIds; // 用于去重
    private PraiseDAO praiseDAO;
    private int color;

    private OnMobClickListener onClickListener;

    public MobListAdapter(Context context) {
        this.mContext = context;
        this.mobOptionsBgColor = context.getResources().getIntArray(R.array.mob_options_bg);
        this.praiseDAO = PraiseDAO.getInstance();
        optionIds = new HashSet<>();
        list = new ArrayList<>();
    }

    //个人完成新增
    public void addNew(QuestionDetailOptions options) {
        if (!optionIds.contains(options.getId())) {
            optionIds.add(options.getId());
            list.add(0, options);
            this.notifyDataSetChanged();
        }
    }

    public void addTop(List<QuestionDetailOptions> topList) {
        optionIds.clear();
        list.clear();
        addBottom(topList);
    }

    public void addBottom(List<QuestionDetailOptions> bottomList) {
        if (addData(bottomList))
            this.notifyDataSetChanged();
    }

    private boolean addData(List<QuestionDetailOptions> dataList) {
        boolean isChange = false;
        for (QuestionDetailOptions options : dataList) {
            if (!optionIds.contains(options.getId())) {
                optionIds.add(options.getId());
                list.add(options);
                isChange = true;
            }
        }
        return isChange;
    }

    @Override
    public int getCount() {
        if (null != list) {
            //外层循环QuestionDetailQuestions,也就是有几个问题。
            int count = (list.size() + 1) / 2;
            return count;
        } else {
            return 0;
        }
    }

    @Override
    public List<QuestionDetailOptions> getItem(int position) {
        if (null != list) {
            List<QuestionDetailOptions> item = null;
            int totalOptionCount = list.size();
            if (totalOptionCount > position * 2 + 1) {
                item = new ArrayList<>(2);
                item.add(list.get(position * 2));
                item.add(list.get(position * 2 + 1));
            } else {
                item = new ArrayList<>(1);
                item.add(list.get(position * 2));
            }
            return item;
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    private HashMap<Integer, Integer> bgColor = new HashMap<>();

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == list) {
            return null;
        }
        List<QuestionDetailOptions> questionDetailOptions = getItem(position);

        MobListViewHolder mobListViewHolder = null;
        if (convertView == null) {
            mobListViewHolder = new MobListViewHolder();
            mobListViewHolder.viewHolder1 = new WaterfallViewHolder();
            mobListViewHolder.viewHolder2 = new WaterfallViewHolder();

            convertView = LayoutInflater.from(mContext).inflate(R.layout.view_moblist_item, null);

            View viewItem1 = ((ViewGroup) convertView).getChildAt(0);
            mobListViewHolder.viewHolder1.itemView = viewItem1;
            mobListViewHolder.viewHolder1.mob_elite_image = (ImageView) viewItem1.findViewById(R.id.mob_elite_image);

            mobListViewHolder.viewHolder1.linearlayoutWaterfallAction = (LinearLayout) viewItem1.findViewById(R.id.linearlayoutWaterfallAction);
            mobListViewHolder.viewHolder1.relativeLayoutWaterfallBg = (RelativeLayout) viewItem1.findViewById(R.id.relativeLayoutWaterfallBg);
            mobListViewHolder.viewHolder1.textViewWaterfallLikedNum = (TextView) viewItem1.findViewById(R.id.textViewWaterfallLikedNum);
            mobListViewHolder.viewHolder1.textViewWaterfallContent = (TextView) viewItem1.findViewById(R.id.textViewWaterfallContent);
            mobListViewHolder.viewHolder1.textViewWaterfallTitle = (TextView) viewItem1.findViewById(R.id.textViewWaterfallTitle);
            mobListViewHolder.viewHolder1.cubeImageViewWaterfallHeader = (ImageView) viewItem1.findViewById(R.id.cubeImageViewWaterfallHeader);
            mobListViewHolder.viewHolder1.cubeImageViewWaterfallLogo = (ImageView) viewItem1.findViewById(R.id.cubeImageViewWaterfallLogo);
            mobListViewHolder.viewHolder1.textViewWaterfallLikedImage = (ImageView) viewItem1.findViewById(R.id.textViewWaterfallLikedImage);
            mobListViewHolder.viewHolder1.mob_fg = (ImageView) viewItem1.findViewById(R.id.mob_fg);

            View viewItem2 = ((ViewGroup) convertView).getChildAt(1);
            mobListViewHolder.viewHolder2.itemView = viewItem2;
            mobListViewHolder.viewHolder2.mob_elite_image = (ImageView) viewItem2.findViewById(R.id.mob_elite_image);
            mobListViewHolder.viewHolder2.linearlayoutWaterfallAction = (LinearLayout) viewItem2.findViewById(R.id.linearlayoutWaterfallAction);
            mobListViewHolder.viewHolder2.relativeLayoutWaterfallBg = (RelativeLayout) viewItem2.findViewById(R.id.relativeLayoutWaterfallBg);
            mobListViewHolder.viewHolder2.textViewWaterfallLikedNum = (TextView) viewItem2.findViewById(R.id.textViewWaterfallLikedNum);
            mobListViewHolder.viewHolder2.textViewWaterfallContent = (TextView) viewItem2.findViewById(R.id.textViewWaterfallContent);
            mobListViewHolder.viewHolder2.textViewWaterfallTitle = (TextView) viewItem2.findViewById(R.id.textViewWaterfallTitle);
            mobListViewHolder.viewHolder2.cubeImageViewWaterfallHeader = (ImageView) viewItem2.findViewById(R.id.cubeImageViewWaterfallHeader);
            mobListViewHolder.viewHolder2.cubeImageViewWaterfallLogo = (ImageView) viewItem2.findViewById(R.id.cubeImageViewWaterfallLogo);
            mobListViewHolder.viewHolder2.textViewWaterfallLikedImage = (ImageView) viewItem2.findViewById(R.id.textViewWaterfallLikedImage);
            mobListViewHolder.viewHolder2.mob_fg = (ImageView) viewItem2.findViewById(R.id.mob_fg);

            convertView.setTag(mobListViewHolder);
        } else {
            mobListViewHolder = (MobListViewHolder) convertView.getTag();
        }

        configViewHolder(mobListViewHolder.viewHolder1, questionDetailOptions.get(0));
        if (questionDetailOptions.size() == 2) {
            mobListViewHolder.viewHolder2.itemView.setVisibility(View.VISIBLE);
            configViewHolder(mobListViewHolder.viewHolder2, questionDetailOptions.get(1));
        } else {
            mobListViewHolder.viewHolder2.itemView.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    private void configViewHolder(WaterfallViewHolder waterfallViewHolder, QuestionDetailOptions options) {
        if (options == null || waterfallViewHolder == null)
            return;

        waterfallViewHolder.linearlayoutWaterfallAction.setTag(options.getId());
        waterfallViewHolder.linearlayoutWaterfallAction.setOnClickListener(this);
        if (options.getPerfect() == 2) {
            waterfallViewHolder.mob_elite_image.setVisibility(View.VISIBLE);
        } else {
            waterfallViewHolder.mob_elite_image.setVisibility(View.GONE);
        }
        waterfallViewHolder.textViewWaterfallTitle.setText(options.getAuthor().getName());
        if (TextUtils.isEmpty(options.getImage())) {
            waterfallViewHolder.cubeImageViewWaterfallLogo.setVisibility(View.GONE);

            if (!bgColor.containsKey(options.getId())) {
                color = mobOptionsBgColor[currentOptionsBgIndex++ >= mobOptionsBgColor.length - 1 ?
                        currentOptionsBgIndex = 0
                        : currentOptionsBgIndex++];
                bgColor.put(options.getId(), color);
                waterfallViewHolder.relativeLayoutWaterfallBg.setBackgroundColor(color);
            } else {
                waterfallViewHolder.relativeLayoutWaterfallBg.setBackgroundColor(bgColor.get(options.getId()));
            }
            waterfallViewHolder.textViewWaterfallContent.setMaxLines(10);
            waterfallViewHolder.textViewWaterfallContent.setGravity(Gravity.CENTER);
            if (options.getContent().length() > 8)
            {
                waterfallViewHolder.textViewWaterfallContent.setTextSize(13);
            }
            else
            {
                waterfallViewHolder.textViewWaterfallContent.setTextSize(20);
            }
            waterfallViewHolder.mob_fg.setVisibility(View.GONE);
        } else {
            waterfallViewHolder.cubeImageViewWaterfallLogo.setVisibility(View.VISIBLE);
            waterfallViewHolder.relativeLayoutWaterfallBg.setBackgroundColor(mContext.getResources().getColor(R.color.gray));
            waterfallViewHolder.textViewWaterfallContent.setMaxLines(3);
            waterfallViewHolder.textViewWaterfallContent.setGravity(Gravity.LEFT);
            ImageUtil.getInstance().displayImage(options.getImage(), waterfallViewHolder.cubeImageViewWaterfallLogo);
            waterfallViewHolder.textViewWaterfallContent.setTextSize(13);
            waterfallViewHolder.mob_fg.setVisibility(View.VISIBLE);
        }

        ImageUtil.getInstance().displayImage(options.getAuthor().getAvatar(), waterfallViewHolder.cubeImageViewWaterfallHeader);
        waterfallViewHolder.textViewWaterfallContent.setText(options.getContent());
        waterfallViewHolder.textViewWaterfallLikedNum.setText(String.format(mContext.getResources().getString(R.string.likeit_number), options.getPraise_count()));
        waterfallViewHolder.textViewWaterfallLikedImage.setSelected(praiseDAO.isPraise(options.getId(), QDEnum.COPEnum.OPTION.value));
    }

    private class MobListViewHolder {
        WaterfallViewHolder viewHolder1;
        WaterfallViewHolder viewHolder2;
    }

    private class WaterfallViewHolder {
        View itemView;
        ImageView mob_elite_image;
        RelativeLayout relativeLayoutWaterfallBg;
        LinearLayout linearlayoutWaterfallAction;
        ImageView cubeImageViewWaterfallLogo;
        ImageView cubeImageViewWaterfallHeader;
        android.widget.TextView textViewWaterfallTitle;
        android.widget.TextView textViewWaterfallContent;
        android.widget.TextView textViewWaterfallLikedNum;
        ImageView textViewWaterfallLikedImage;
        ImageView mob_fg;
    }

    public void updatePraiseNumber(int selectId, int likeNumber) {
        int count = list.size();
        for (int i = 0; i < count; i++) {
            QuestionDetailOptions questionDetailOptions = list.get(i);
            if (questionDetailOptions.getId() == selectId) {
                questionDetailOptions.setPraise_count(likeNumber);
                this.notifyDataSetChanged();
                break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.linearlayoutWaterfallAction:
                if (onClickListener != null) {
                    onClickListener.onClickPraise((int) v.getTag());
                }
                break;
        }
    }

    public OnMobClickListener getOnClickListener() {
        return onClickListener;
    }

    public void setOnMobClickListener(OnMobClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnMobClickListener {
        void onClickPraise(int optionId);

        void onClickOption(int optionId);
    }
}
